import java.sql.SQLException;
import java.util.Date;
import java.util.Scanner;

class admin {

	public admin() {

		try {
			Showlist.adminList();
			System.out.println("------------------------------------");
			System.out.println("1. Create administrator or staff");
			System.out.println("2. Edit administrator or staff");
			System.out.println("3. Delete administrator or staff");
			System.out.println("4. Change password");
			System.out.println("5. Exit");
			System.out.println("------------------------------------");
			int choice;
			String username;
			String password;
			String role;
			String firstname = null;
			String lastname = null;
			boolean license;
			int uid;
			Date d = new Date();
			Date nextyear = new Date();
			nextyear.setTime(d.getTime() + 1 * 24 * 60 * 60 * 1000);
			Scanner input = new Scanner(System.in);
			System.out.println("choice:");
			choice = input.nextInt();

			while (choice != 5) {
				switch (choice) {
				case 1:
					System.out.println("Please enter username: ");
					username = input.next();
					if (Check.checkUsername(username)) {// check
						// facilityid
						System.out.println("username already exist!");
						break;
					}
					else {
					System.out.println("Please enter password: ");
					password = input.next();
					System.out.println("Please enter first name: ");
					firstname = input.next();
					System.out.println("Please enter last name: ");
					lastname = input.next();
					System.out.println("Please enter user role: ");					
					role = input.next();
					while (!((role.equals("ADMIN")) || (role.equals("STAFF")))) {
						System.out.println("User type must be ADMIN or STAFF! Please re-enter: ");
						role = input.next();
					}
					User usrCreate = new User(username,password,firstname,lastname,role,false,null);
					usrCreate.insert();
					System.out.println("Create User successful");
					}
					break;
				case 2:
					Showlist.adminList();
					Showlist.staffList();
					System.out.println("Please enter user id: ");
					uid = input.nextInt();
					if (!Check.checkUserID(uid)) {// check
						System.out.println("user does not exist!");
						break;
					}
					else {
					User usr = new User(uid);
					System.out.println("Please enter password: ");
					password = input.next();
					usr.setPassword(password);
					System.out.println("Please enter user type: ");
					role = input.next();
					usr.setRole(role);
					System.out.println("Please enter first name: ");
					firstname = input.next();
					usr.setFirstName(firstname);
					System.out.println("Please enter last name: ");
					lastname = input.next();
					usr.setLastName(lastname);
					usr.update();
					System.out.println("Update User successful");
					}
					break;
				case 3:
					Showlist.adminList();
					Showlist.staffList();
					System.out.println("Please enter UID: ");

					uid = input.nextInt();
					if (!Check.checkUserID(uid)) {// check
						System.out.println("user does not exist!");
						break;
					}
					User usr2 = new User(uid);
					usr2.delete();
					System.out.println("Delete User successful");

					break;
				case 4:
					System.out.println("Please enter new password: ");

					password = input.next();

					
					while(!PasswordCheck.checkPassword(password)) {
						System.out .println(
						"A valid password must contain at least 8 characters and must include at least a number and a letter. "
						); System.out.println("Please re-enter new password: ");
						password = input.next(); }
					

					User user = new User(Login.getUserId());
					user.setPassword(password);
					User.update();
					System.out.println("Password Update successful");

					break;
				case 5:
					System.out.println("Bye!");
					System.exit(0);
					break;
				default:
					System.out.println("Invaild Input, please enter 1,2 or 3.");

				}
				System.out.println("------------------------------------");
				System.out.println("1. Create administrator or staff");
				System.out.println("2. Edit administrator or staff");
				System.out.println("3. Delete administrator or staff");
				System.out.println("4. Change password");
				System.out.println("5. Exit");
				System.out.println("------------------------------------");
				System.out.println("choice:");
				choice = input.nextInt();

			}

		}

		catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
